package pinyinJava.utils;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;

public class MyTimerTask extends TimerTask {

    private Timer timer;
    private AtomicInteger atomicInteger;
    private int total;
    private ExecutorService executorService;
    private int wroteInt = -1;

    public MyTimerTask(Timer timer, AtomicInteger atomicInteger, int total, ExecutorService executorService) {
        this.timer = timer;
        this.atomicInteger = atomicInteger;
        this.executorService = executorService;
        this.total = total;
    }

    @Override
    public void run() {
        if (wroteInt != atomicInteger.get()) {
            wroteInt = atomicInteger.get();
            System.out.println(String.format("executing...%d/%d", wroteInt, total));
        }
        if (atomicInteger.get() == total) {
            timer.cancel();
            timer.purge();
            executorService.shutdown();
            System.out.println("done");
        }
    }
}
